import numpy as np
from sklearn.cluster import KMeans
from sklearn.manifold import TSNE
from sklearn.decomposition import PCA
from sklearn.tree import DecisionTreeClassifier
from .IMM import createClusterTree,getOrderFeature


def readData():
    with open("data2.csv", encoding='utf-8') as f:
        data = np.loadtxt(f, str,delimiter=',')
        print(data[5,:])
        return data[0,1:8],data[1:1000,1:8]
    pass


def readStudentInfo():
    with open("StudentInfo.csv", encoding='utf-8') as f:
        data = np.loadtxt(f, str,delimiter=',')
        print(data[5,:])
        return data[0,1:7],data[1:,1:7]
    pass

def readStudentScore():
    with open("score.csv", encoding='utf-8') as f:
        data = np.loadtxt(f, str,delimiter=',')
        print(data[5,:])
        return data[0,1:8],data[1:,1:8]
    pass

def readCSV(file):
    with open(file, encoding='utf-8') as f:
        data = np.loadtxt(f, str,delimiter=',')
        print(data[5,:])
        return data[0,1:8],data[1:,1:8],data[1:,9]

def kmeanscluster(clusternum,data):
    model = KMeans(n_clusters=clusternum)
    y_pred = model.fit_predict(data)
    return y_pred


def showTSNEView(data,n_feature):
    data = np.array(data)
    result = TSNE(n_components=2, learning_rate=100).fit_transform(data)
    return result


def showPCAView(data,n_feature):
    data = np.array(data)
    result = PCA(n_components=2).fit_transform(data)
    return result

def showAttributeTSNE(data,attribute):
    data = np.array(data)
    attribute = int(attribute)
    data_before = data[:,:attribute]
    data_after = data[:,attribute+1:]
    attributeData = data[:,attribute]
    attributeData = attributeData.reshape((data.shape[0],1))
    data = np.hstack((data_before, data_after))
    result = TSNE(n_components=1, learning_rate=100).fit_transform(data)
    result = np.hstack((attributeData, result))
    return result


def makeRuleTree(data,typeArray,featureImport):
    dic =  createClusterTree(data,typeArray,featureImport)
    # data_y = typeArray
    # dtc = DecisionTreeClassifier(criterion='entropy', max_depth=2)  # 建立决策树对象
    # dtc.fit(data, data_y)  # 决策树拟合
    # dic = {}
    # dic['feature'] = dtc.tree_.feature.tolist()
    # dic['children_right'] = dtc.tree_.children_right.tolist()
    # dic['children_left'] = dtc.tree_.children_left.tolist()
    # dic['value'] = dtc.tree_.value.tolist()
    # dic['threshold'] = dtc.tree_.threshold.tolist()
    return dic


def getFeatureOrder(data,type,center):
    dic ={}
    bestValues = getOrderFeature(data,type,center)
    dic['data'] = bestValues
    return dic

# if __name__ == '__main__':
#     readCSV("../hero_data.csv")